import Vue from "vue";
import VueRouter from "vue-router";

import Container from "@/views/Container.vue";
import Index from "@/views/Index.vue";
import School from "@/views/School.vue";
import Major from "@/views/Major.vue";
import Simulation from "@/views/Simulation.vue";
import User from "@/views/User.vue";
import Login from "@/views/Login.vue";

Vue.use(VueRouter);

const routes = [
    {
        path: "/login",
        name: "login",
        component: Login,
        meta: { requiresAuth: false },
    },
    {
        path: "/",
        name: "container",
        component: Container,
        meta: { requiresAuth: true },
        children: [

            {
                path: "/index",
                name: "index",
                component: Index,
                meta: { requiresAuth: true },
            },


            {
                path: "/school",
                name: "school",
                component: School,
                meta: { requiresAuth: true },

            },

            {
                path: "/major",
                name: "major",
                component: Major,
                meta: { requiresAuth: true },

            },

            {
                path: "/simulation",
                name: "simulation",
                component: Simulation,
                meta: { requiresAuth: true },

            },

            {
                path: "/user",
                name: "user",
                component: User,
                meta: { requiresAuth: true },

            },

        ]
    },

];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes
});

router.beforeEach((to, from, next) => {
    // 检查 to.matched 中的路由记录，判断用户是否有权限
    const token = sessionStorage.getItem('token');
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // 这里判断用户是否登录，具体逻辑依项目而定
        if (!token) {
            // 用户未登录，重定向到登录页面
            next({ path: '/login' });
        } else {
            next();
        }
    } else {
        next();
    }
});

export default router;